package com.zhuiyun.pm.client.address.service.servicelmpl;

import com.zhuiyun.pm.client.address.entity.ClientAddress;
import com.zhuiyun.pm.client.address.mapper.AddressMapper;
import com.zhuiyun.pm.client.address.service.AddressService;
import com.zhuiyun.pm.client.login.entity.ClientUser;
import com.zhuiyun.pm.client.login.mapper.UserLoginDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName AddressServicelmpl
 * Description 发布service实现
 * Author Administrator
 * @Date 2023/3/14
 * Version 1.0
 **/
@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    AddressMapper dddressMapper;
    @Autowired
    UserLoginDao userLoginDao;
    /**
     * 买家地址查询
     * @author 温
     * @date 2023/3/16 11:15
     * @parameter [clientAddress]
     * @return java.util.List<com.zhuiyun.pm.client.dizhi.entity.ClientAddress>
     */

    @Override
    public List<ClientAddress> clientAddressList(ClientAddress clientAddress) {
        List<ClientAddress> clientAddresses = dddressMapper.clientAddressList(clientAddress);
        return clientAddresses;
    }

    /**
     * 地址添加
     * @author 温
     * @date 2023/3/14 19:15
     * @parameter [clientAddress]
     * @return int
     */
    @Override
    public int clientAddressAdd(ClientAddress clientAddress) {
        clientAddress.setDelFlag(0);
        int i = dddressMapper.insertSelective(clientAddress);
        ClientUser clientUser = userLoginDao.selectByPrimaryKey(clientAddress.getUserId());
        if(clientUser.getUserDefaultAddrId()==null){
            clientUser.setUserDefaultAddrId(clientAddress.getId());
            userLoginDao.updateByPrimaryKeySelective(clientUser);
        }

        return i;
    }
    /**
     * 买家地址修改
     * @author 温
     * @date 2023/3/14 19:23
     * @parameter [clientAddress]
     * @return int
     */
    @Override
    public int clientAddressUpdate(ClientAddress clientAddress) {
        int i = dddressMapper.updateByPrimaryKeySelective(clientAddress);
        return i;
    }

    /**
     * 删除买家地址
     * @author 温
     * @date 2023/3/15 9:28
     * @parameter [id]
     * @return int
     */
    @Override
    public int delAddressInfo(int id) {
        int i = dddressMapper.delAddressInfo(id);
        return 0;
    }
    @Override
    public ClientAddress getDefaultAddByUserId(Integer userId) {
        ClientAddress clientAddress = new ClientAddress();
        clientAddress.setUserId(userId);
        clientAddress.setDefaultFlag(1);
        return dddressMapper.getDefaultAddByUserId(clientAddress);
    }

    @Override
    public void updateUserDefaultAddr(Integer userId ,Integer AddrId) {
        ClientUser clientUser = new ClientUser();
        clientUser.setId(userId);
        clientUser.setUserDefaultAddrId(AddrId);
        userLoginDao.updateByPrimaryKeySelective(clientUser);
    }
//    /**
//     * 根据id查询地址详情
//     * @author 温
//     * @date 2023/3/15 9:24
//     * @parameter [id]
//     * @return com.zhuiyun.pm.client.dizhi.entity.ClientAddress
//     */
//    @Override
//    public ClientAddress clientId(int id) {
//        ClientAddress clientAddress = dddressMapper.selectByPrimaryKey(id);
//        return clientAddress;
//    }
}
